//
//  WNHomeReweetView.swift
//  XLWeibo
//
//  Created by 风不会停息 on 2017/7/28.
//  Copyright © 2017年 风不会停息. All rights reserved.
//

import UIKit
import SnapKit

class WNHomeReweetView: UIView {

    var retweetViewBottomConstraint: Constraint?

    var statusViewModel : WNStatusViewModel?{
        
        didSet{
            
            contentLabel.text = statusViewModel?.retweetContent
            
            //  卸载之前的底部约束
            retweetViewBottomConstraint?.deactivate()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  表示有转发微博对应的配图
                //  1. 显示配图
                pictureView.isHidden = false
                //  2. 重新冲击转发微博视图底部约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(WNCellMargin).constraint
                })
                //  给配图绑定数据
                pictureView.picUrls = picUrls
            } else {
                //  表示没有转发微博对应的配图
                //  1. 隐藏配图
                pictureView.isHidden = true
                //  2. 重新冲击转发微博视图底部约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(WNCellMargin).constraint
                })
            }

        }
    }
    
    //  MARK: --懒加载控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 13, textColor: UIColor.lightGray)
        //  多行显示
        label.numberOfLines = 0
        return label
        
    }()
    
    //  配图
    fileprivate lazy var pictureView: WNHomePictureView = {
        let collectionView = WNHomePictureView()
        //  配图颜色和转发微博颜色一样
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()


    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(WNCellMargin)
            make.trailing.equalTo(self).offset(-WNCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(WNCellMargin)
            make.leading.equalTo(contentLabel)
            //  设置默认大小，后续根据图片张数来确定真是的配图大小
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        //  关键约束
        self.snp.makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(WNCellMargin).constraint
        }
        
    }

}
